import constants as con
import pygame
from pygame.locals import *

import scoreboard
import instructions
import menubg
import title

class Menu(object):
    def __init__(self,parent):
        self.parent = parent
        self.font = parent.font
        self.options = [ 'Start Game', 'Instructions', 'Scoreboard', 'Quit' ]
        self.selected = 1

        self.x = int(con.window_size[0] * 0.30) 
        self.y = int(con.window_size[1] * 0.25)

        self.pos = (self.x,self.y)

        self.sprites = pygame.sprite.LayeredUpdates()

        # here's hoping we're using fixed width
        (self.width,self.height) = self.font.size('A')
        self.line_length = int( (con.window_size[0] - (self.pos[0] * 2)) / self.width)

        self.bg = menubg.BG(self)
        self.title = title.Title(self)
        self.menunoise = con.load_sound('Brick.wav')
      
    def redraw(self):

        self.sprites.draw(self.parent.screen)
        cursorpos = 1
        x = self.pos[0]
        y = self.pos[1]
        for opt in self.options:
            textbuf = str(cursorpos)+". "+opt
            label = self.font.render(textbuf, True, con.fgcolor)
            self.parent.screen.blit(label, (x,y))

            if cursorpos == self.selected:
                cursor = self.font.render('>', True, con.fgcolor)
                self.parent.screen.blit(cursor, (x - 20,y))

            y += self.height
            cursorpos += 1

        y += self.height * 2
        textbuf = "BPM: %d (higher is more difficult)" % self.parent.bpm
        label = self.font.render(textbuf, True, con.fgcolor)
        self.parent.screen.blit(label, (x,y))

        y += self.height * 3
        y = self.stringdraw("Use arrow keys to navigate, or type a number. Left/right keys control BPM.", x, y)

        y += self.height * 3
        self.stringdraw("Press ESCAPE at any time to exit game.", x, y)


    def stringdraw(self,str,x,y):

        buf = ''
        for word in str.split():
            # render if adding this word would put us over the limit
            if (len(buf) + len(word)) >= self.line_length:
                label = self.font.render(buf, True, con.fgcolor)
                self.parent.screen.blit(label, (x,y))
                y += self.height
                buf = word
            elif len(buf) == 0:
                buf = word
            else:
                buf = buf + ' ' + word

        if len(buf):
            label = self.font.render(buf, True, con.fgcolor)
            self.parent.screen.blit(label, (x,y))

        return y

    def inputUp(self,key):
        if key == K_LEFT or key == K_RIGHT:
            self.parent.bpmadjusting = 0

    def inputDown(self,key):
        if key == K_1:
            self.select_from_menu(1)
        elif key == K_2:
            self.select_from_menu(2)
        elif key == K_3:
            self.select_from_menu(3)
        elif key == K_4:
            self.select_from_menu(4)
        elif key == K_UP:
            self.selected = self.selected - 1
            if self.selected < 1:
                self.selected = len(self.options)
            elif self.selected > len(self.options):
                self.selected = 1
            self.menunoise.play()
        elif key == K_DOWN:
            self.selected = self.selected + 1
            if self.selected < 1:
                self.selected = len(self.options)
            elif self.selected > len(self.options):
                self.selected = 1
            self.menunoise.play()
        elif key == K_LEFT:
            self.parent.bpmadjusting = -1
        elif key == K_RIGHT:
            self.parent.bpmadjusting = 1
        elif key == K_RETURN:
            self.select_from_menu(self.selected)

    def select_from_menu(self,input):
        self.selected = input

        if self.selected == 1:
            print "game start"
            self.parent.reset()
            self.parent.state = 'Play'
            pygame.mixer.music.play(-1,0.0)
            print "BPM is ", self.parent.bpm
            self.parent.beat = 60000 / self.parent.bpm
            print "A beat will hit every %d millisec." % (self.parent.beat)
        elif self.selected == 2:
            if self.parent.instructions == None:
                self.parent.instructions = instructions.Instructions(self.parent)
            self.parent.state = 'Instructions'
        elif self.selected == 3:
            self.parent.topscores = scoreboard.Scoreboard(self.parent)
            self.parent.state = 'Scores'
        elif self.selected == 4:
            self.parent.exit = True

